using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace StudentManagementApplication
{
    public partial class FrmChangePassword : Form
    {
        Business.LoginService loginService = new Business.LoginService();
        Entities.Login account = null;
        public string sLoginID;

        public FrmChangePassword()
        {
            InitializeComponent();
        }


        private void FrmChangePassword_Load(object sender, EventArgs e)
        {

        }

        private bool Test()
        {
            bool bResult = true;
            string sInformation = string.Empty;
            if (txtOldPassword.Text == string.Empty)
                sInformation += "The old password is empty" + "\n";
            if (txtNewPassword.Text == string.Empty)
                sInformation += "The new password is empty" + "\n";
            if (txtReNewPassword.Text == string.Empty)
                sInformation += "The pre-typed new password is empty" + "\n";
            if(!CheckOldPassword(txtOldPassword.Text.Trim()))
                sInformation += "Invalid password" + "\n";
            if(txtNewPassword.Text.Length < 7)
                sInformation += "Password have at least seven chars" + "\n";
            if(!CheckNewPassword())
                sInformation += "New password and re-typed new password doesn't match" + "\n";
            if(sInformation != string.Empty)
            {
                bResult = false;
                MessageBox.Show(sInformation,"Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            return bResult;
        }

        private bool CheckOldPassword(string sOldPassword)
        {
            account = new Entities.Login();
            account = loginService.GetAccountByLoginID(sLoginID);
            if(account.Password == txtOldPassword.Text.Trim())
            {
                return true;
            }
            return false;
        }

        private bool CheckNewPassword()
        {
            return (txtNewPassword.Text.Trim() == txtReNewPassword.Text.Trim() ? true : false);
        }

        private void UpdatePassword()
        {
            if (Test())
            {
                account.Password = txtReNewPassword.Text.Trim();
                loginService.UpdatePasswordOfAccount(account);
                MessageBox.Show("Password 's changed", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UpdatePassword();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtOldPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
                UpdatePassword();
        }

        private void txtNewPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13) UpdatePassword();
        }

        private void txtReNewPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13) UpdatePassword();
        }
    }
}